﻿<?php
    function connectDB(){
    $conn = new mysqli('localhost','root','');
    if (!$conn) {
        die("failed");
    }
    $conn->query("SET NAMES UTF8");
    $conn->select_db('wifistation') or die("connot find");
    //@mysql_query("SET NAMES UTF8");
    //@mysql_select_db('wifistation', $conn) or die("connot find");
    return $conn;
    }

    function reverse($array){
    $size = count($array);

    for($i=0;$i<=floor($size/2);$i++){
        $b = $array[$size-$i-1];
        $array[$size-$i-1] = $array[$i];
        $array[$i] = $b;
    }
    return $array;

}

$conn = connectDB();

$result_tem = $conn->query('SELECT temperature FROM weatherdata ORDER BY id DESC LIMIT 1');

$result_tem_arr = array();   //存储数组
while ($rows = mysqli_fetch_array($result_tem)) {      //$rows是数组
    settype($rows['temperature'], 'float');          //string变成float
    $result_tem_arr[] = $rows['temperature'];        //提取赋值
}
$result_tem_arr = reverse($result_tem_arr);
$json_tem = json_encode($result_tem_arr);       //转化为json格式

?>





